package cn.tedu.fruitshop.controller;


import cn.tedu.fruitshop.pojo.vo.AddressListItemVO;
import cn.tedu.fruitshop.pojo.vo.RoleListItemVO;
import cn.tedu.fruitshop.pojo.vo.UserListItemVO;
import cn.tedu.fruitshop.service.RoleServer;
import cn.tedu.fruitshop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "01. 角色查询")
@RestController
@Validated
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleServer roleServer;

    @GetMapping("/user")
    @ApiOperation("用户列表")
    public JsonResult<List<UserListItemVO>> userList(){
        log.debug("开始处理用户查询列表请求");
        List<UserListItemVO> userListItemVOS = roleServer.userList();
        log.debug("查询的数据是{}",userListItemVOS);
        return JsonResult.ok(userListItemVOS);
    }

    @ApiOperationSupport(order = 100)
    @ApiOperation("根据id删除数据")
    //http://localhost:8080/role/{id}/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Integer")
    })
    public JsonResult<Void>delete(@Range(min = 1,message = "添加用户失败，尝试删除时用户的id失效")  @PathVariable Integer id){

        log.debug("开始处理[根据id删除用户]的请求,参数:[]",id);
        roleServer.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("/business")
    @ApiOperation("卖家列表")
    public JsonResult<List<UserListItemVO>> businessList(){
        log.debug("开始处理卖家查询列表请求");
        List<UserListItemVO> list = roleServer.businessList();
        log.debug("查询的数据是{}",list);
        return JsonResult.ok(list);
    }

    @GetMapping("/admin")
    @ApiOperation("买家列表")
    public JsonResult<List<UserListItemVO>> adminList(){
        log.debug("开始处理买家查询列表请求");
        List<UserListItemVO> list = roleServer.adminList();
        log.debug("查询的数据是{}",list);
        return JsonResult.ok(list);
    }




}
